const models = require('../../models');
const sequelize = models.db1;
const sql_pool = require('../sql_pool/achievingRate_sql');
const deriveData = require('../../middle/common/utility/deriveData2')
const deriveData2 = require('../../middle/common/utility/deriveData2')
const verify = require('../../middle/common/utility/verify')


module.exports = {
  AchievingRateQuanwangExcel: async (ctx) => {
    const params = ctx.request.body
    const sql = sql_pool.AchievingRateQuanwang_sql(params); // sql语句
    const res = await sequelize.query(sql.sql, {
      type: sequelize.QueryTypes.SELECT
    })
    let title = [
      '日期', '始发区域', '目的区域', '区县时效', '时效达成票量', '时效未达成票量', '总票量', '达成率'
    ]
    const data = await deriveData.FsWrite(params, Object.values(res[1]), title, ctx)
    console.log(data)
    ctx.body = {
      data: data,
      code: 1,
      msg: "success"
    }
  },
  // 经营指标-转运及时率明细
  transferTimelinessDetailRate: async (ctx) => {
    const params = ctx.request.body
    const sql = sql_pool.transferTimelinessDetailRate_sql(params); // sql语句
    const limit_sql = sql_pool.limit_sql(params); // sql语句
    const res = await sequelize.query(sql + limit_sql, {
      type: sequelize.QueryTypes.SELECT
    })
    const data = {
      records: Object.values(res[1]),
      current: params.current + 1,
      size: params.size,
      total: res[0]['0']['count(1)'],
    }
    ctx.body = {
      data: data,
      code: 1,
      msg: "success"
    }
  },
  transferTimelinessDetailRateExcel: async (ctx) => {
    const params = ctx.request.body
    const sql = sql_pool.transferTimelinessDetailRate_sql(params); // sql语句
    const res = await sequelize.query(sql, {
      type: sequelize.QueryTypes.SELECT
    })
    let title = [
      '日期', '运单号', '签收省份', '签收城市', '签收区县', '签收网点', '寄件网点', '寄件网点编码', '寄件代理区', '寄件代理区编码',
      '始发转运中心', '始发转运中心编码', '上一站转运中心', '上一站转运中心编码', '上一站代理区', '上一站代理区编码',
      '派件代理区', '中心所属代理区', '末端转运中心',
      '到车任务编号', '到车时间', '发车时间', '下一站', '应散货时间', '转运是否及时'
    ]
    const data = await deriveData2.FsWrite(params, Object.values(res[1]), title, ctx)
    // console.log(data)
    ctx.body = {
      data: data,
      code: 1,
      msg: "success"
    }
  },
  shiyanExcel: async (ctx) => {
    const params = ctx.request.body
    // const sql = sql_pool.AchievingRateQuanwang_sql(params); // sql语句
    // const res = await sequelize.query(sql.sql, {
    //   type: sequelize.QueryTypes.SELECT
    // })
    function createTabsRows() {
      const arr = [];
      for (let i = 0; i < 1500000; i++) {
        arr.push(['jjj', '222', 3333])
      }
      return arr
    }
    let title = [
      '日期', '始发区域', '目的区域'
    ]
    const data = await deriveData2.FsWrite(params, createTabsRows(), title, ctx)
    console.log(data)
    ctx.body = {
      data: data,
      code: 1,
      msg: "success"
    }
  }
}


